﻿using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Emailing;
using Volo.Abp.SettingManagement;

namespace Volo.Abp.Extension.Web.Pages.SettingManagement.EmailSetting
{
    public class EmailSettingViewComponent : AbpViewComponent
    {
        private ISettingManager settingManager;

        public EmailSettingViewComponent(ISettingManager settingManager)
        {
            this.settingManager = settingManager;
        }


        public async Task<IViewComponentResult> InvokeAsync()
        {
            var model = new EmailSettingDto();

            model.DefaultFromDisplayName = await this.settingManager.GetOrNullGlobalAsync(EmailSettingNames.DefaultFromDisplayName);
            model.DefaultFromAddress = await this.settingManager.GetOrNullGlobalAsync(EmailSettingNames.DefaultFromAddress);
            model.SmtpHost = await this.settingManager.GetOrNullGlobalAsync(EmailSettingNames.Smtp.Host);
            model.SmtpPort = await this.settingManager.GetOrNullGlobalAsync(EmailSettingNames.Smtp.Port);
            model.SmtpUserName = await this.settingManager.GetOrNullGlobalAsync(EmailSettingNames.Smtp.UserName);
            model.SmtpPassword = await this.settingManager.GetOrNullGlobalAsync(EmailSettingNames.Smtp.Password);
            model.SmtpDomain = await this.settingManager.GetOrNullGlobalAsync(EmailSettingNames.Smtp.Domain);
            model.SmtpEnableSsl = bool.Parse(await this.settingManager.GetOrNullGlobalAsync(EmailSettingNames.Smtp.EnableSsl));
            model.SmtpUseDefaultCredentials = bool.Parse(await this.settingManager.GetOrNullGlobalAsync(EmailSettingNames.Smtp.UseDefaultCredentials));

            return View("~/Pages/SettingManagement/EmailSetting/Default.cshtml", model);
        }
    }
}
